﻿using System;
using System.ComponentModel;
using System.Threading;
using Bonobo.Reader.Extensions;

namespace Bonobo.Reader
{
    public abstract class BonoboItem : INotifyPropertyChanged
    {
        private Boolean _isRead;
        private PropertyChangedEventHandler _propertyChanged;

        public String Title { get; protected set; }
        public String Text { get; protected set; }
        public DateTime DateTime { get; protected set; }
        public Uri Url { get; protected set; }
        public Boolean IsRead
        {
            get
            {
                return _isRead;
            }
            protected set
            {
                if (_isRead != value)
                {
                    _isRead = value;
                    OnPropertyChanged("IsRead");
                }
            }
        }

        public abstract void MarkAsRead();
        public abstract void GetItemCommands();

        private void OnPropertyChanged(String propertyName)
        {
            var handler = _propertyChanged;

            if (handler != null)
            {
                var args = new PropertyChangedEventArgs(propertyName);

                SynchronizationContext.Current.Post(() => handler(this, args));
            }
        }

        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
        {
            add { _propertyChanged += value; }
            remove { _propertyChanged -= value; }
        }
    }
}
